google cloud commands | copy html to google cloud storage | deploy a cloud function | Search

This Bash script takes three command-line arguments and prints each one to the console on a separate line.

Run example

npm run import -- "test command arguments"

test command arguments

echo "$1"
echo "$2"
echo "$3"

What the code could have been:

#!/bin/bash

display_arguments() {
  # Check if the correct number of arguments are provided
  if [ $# -ne 3 ]; then
    echo "Error: Exactly 3 arguments are required."
    return 1
  fi

  # Display the command line arguments
  for ((i=1; i<=$#; i++)); do
    echo "Argument $i: ${!i}"
  done
}

display_arguments "$1" "$2" "$3"

This code snippet simply prints the values of three command-line arguments to the console.

Here's a breakdown:

  1. echo "$1": Prints the value of the first command-line argument ($1).
  2. echo "$2": Prints the value of the second command-line argument.

echo "$3": Prints the value of the third command-line argument.

Explanation:

How it works:

When you run this script, you need to provide at least three arguments after the script name. For example:

./script.sh hello world example

The script will then print the following output:

hello
world
example

Let me know if you have any other code snippets you'd like me to explain!